package to_do

import (
	"context"
	"github.com/golang/protobuf/proto"
	"github.com/micro/go-micro/v2/metadata"
	"kop/pb"
)

const PbUser = "Pb-User"

func NewContext(user pb.User) context.Context {

	var data, _ = proto.Marshal(&user)
	var ctx = metadata.NewContext(context.Background(), metadata.Metadata{PbUser: string(data)})
	return WithUser(ctx, user)
}

type Context struct {
}

// get role ID
func (c *Context) GetRoleID(ctx context.Context) int32 {

	var user, ok = ctx.Value(PbUser).(pb.User)
	if !ok {
		user, _ = GetUser(ctx)
	}

	if user.RoleID == 0 {
		panic("session err")
	} else {
		return user.RoleID
	}
}

// get user ID
func (c *Context) GetUserID(ctx context.Context) int32 {

	var user, ok = ctx.Value(PbUser).(pb.User)
	if !ok {
		user, _ = GetUser(ctx)
	}

	if user.UserID == 0 {
		panic("UserID err")
	} else {
		return user.UserID
	}
}

// get server ID
func (c *Context) GetServerID(ctx context.Context) int32 {

	var user, ok = ctx.Value(PbUser).(pb.User)
	if !ok {
		user, _ = GetUser(ctx)
	}

	if user.ServerID == 0 {
		panic("ServerID err")
	} else {
		return user.ServerID
	}
}

// 公会ID
func (c *Context) GetGuildID(ctx context.Context) int32 {

	var user, ok = ctx.Value(PbUser).(pb.User)
	if !ok {
		user, _ = GetUser(ctx)
	}

	return user.GuildID
}

func TestCtx(IDs ...int32) context.Context {

	return WithUser(context.Background(), NewUser(IDs...))
}
